import React, { useEffect, useImperativeHandle } from 'react'
import FieldContext from './Context';
import useForm from './useForm';

// ref  key 是react保留关键字 不能直接使用props传递
const Form = ({form, children, onFinish, onFinishFailed}, ref) => {
  const [formuInstance] = useForm(form)

  useImperativeHandle(ref, () => formuInstance) // useImperativeHandle 可以使用ref是自定保留给父组件的实例值

  formuInstance.setCallbacks({
    onFinish,
    onFinishFailed
  })
  return (
    <form onSubmit={(e) => {
      e.preventDefault()
      formuInstance.sumbit()
    }}>
      <FieldContext.Provider value={formuInstance}>
        {children}
      </FieldContext.Provider>
    </form>
  )
}
export default Form